package com.zoweunion.mechanic.service.base;

import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.service.app.AppService;
import com.zoweunion.mechanic.util.Page;
import com.zoweunion.mechanic.util.UuidUtil;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.constants.PushConstants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class BaseService {
    @Autowired
    private AppService appService;

    /**
     * 设置分页的limit和offset
     *
     * @param reqMap
     */
    public Map<String, Object> appPage(Map<String, Object> reqMap) {
        Map<String, Object> pageMap = Page.appPage(reqMap.get("pageSize").toString(),
                reqMap.get("currentPage").toString());
        reqMap.put("pageSize", pageMap.get("limit"));
        reqMap.put("currentPage", pageMap.get("offset"));
        return pageMap;
    }

    protected void pageInfo(Map<String, Object> resultMap, List<Map<String, Object>> list, int count, int pageSize) {
        //页数
        int pages = this.getPages(count, pageSize);
        resultMap.put("count", count);
        resultMap.put("pages", pages);
        resultMap.put("list", list);
    }

    protected int getPages(int count, int pageSize) {
        return count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
    }

    public void setBasicParams(User currentUser, Map<String, Object> map){
        map.put("create_user", currentUser.getId());
        map.put("update_user", currentUser.getId());
    }

    public boolean isGuest(User currentUser) {
        return currentUser == null || currentUser.getR_id().equals(Constants.ROLE_YK);
    }


    public boolean isSuperAdmin(String account) {
        return Arrays.asList(Constants.SUPER_ADMIN).contains(account);
    }

    /**
     * 获取工单尾号
     */
    public String getOrderNoEndNumber(String orderNo){
        return orderNo.substring(orderNo.length() - 4);
    }

    /**
     * 在reqMap中增加当前用户的管辖区域
     *
     * @param currentUser
     * @param reqMap
     */
    public void setPrecinctRegionsInReqMap(User currentUser, Map<String, Object> reqMap) {
        String precinctRegions = "";
        if (!Constants.ROLE_GLY.equals(currentUser.getR_id())) {
            reqMap.put("isGLY", false);
            if (reqMap.get("precinctRegions") == null || StringUtils.isBlank(reqMap.get("precinctRegions").toString())) {
                precinctRegions = String.join(",", appService.getPrecinctRegionNumbers(currentUser.getId(), currentUser.getS_id()));
            } else {
                String precinctRegionP = String.join(",", appService.getPrecinctRegionNumbers(currentUser.getId(), currentUser.getS_id()));
                if (precinctRegionP.contains(reqMap.get("precinctRegions").toString())) {
                    precinctRegions = reqMap.get("precinctRegions").toString();
                }
            }
            if (StringUtils.isBlank(precinctRegions)) {
                precinctRegions = "没有分配区域";
            }
        } else {
            reqMap.putIfAbsent("isGLY", true);
            if (reqMap.get("precinctRegions") == null || StringUtils.isBlank(reqMap.get("precinctRegions").toString())) {
                precinctRegions = String.join(",", appService.getPrecinctRegionNumbers(null, currentUser.getS_id()));
            } else {
                String precinctRegionP = precinctRegions = String.join(",", appService.getPrecinctRegionNumbers(null, currentUser.getS_id()));
                if (precinctRegionP.contains(reqMap.get("precinctRegions").toString())) {
                    precinctRegions = reqMap.get("precinctRegions").toString();
                }
            }
        }
        reqMap.put("precinctRegions", precinctRegions);
    }


    public Map<String, Object> pushMessageOrderNewsParams(String pushUserId, String pushTime, String pushContent, String sId) {
        Map<String, Object> extraParams = new HashMap<>();
        extraParams.put("s_id", sId);
        return pushMessageParamsHasId("1", pushUserId, PushConstants.TITLE_SYSTEM, pushTime, pushContent, 1, "0", 2, "system", "system", extraParams);
    }

    public Map<String, Object> pushMessageCarNewsParams(String pushUserId, String pushTime, String pushContent, String sId) {
        Map<String, Object> extraParams = new HashMap<>();
        extraParams.put("s_id", sId);
        return pushMessageParamsHasId("1", pushUserId, PushConstants.TITLE_SYSTEM, pushTime, pushContent, 2, "0", 3, "system", "system", extraParams);
    }

    public Map<String, Object> pushMessageOrderNodeRemindParams(String pushUserId, String pushTime, String pushContent, String sId, String orderId, Integer orderStatus) {
        Map<String, Object> extraParams = new HashMap<>();
        extraParams.put("s_id", sId);
        extraParams.put("order_id", orderId);
        extraParams.put("order_status", orderStatus);
        return pushMessageParams("1", pushUserId, PushConstants.TITLE_ORDER_REMIND, pushTime, pushContent, 3, "0", 2, "system", "system", extraParams);
    }

    public Map<String, Object> pushMessagePublishCheckParams(String pushUserId, String pushTime, String pushContent, String pushKey, String pushExtraValue) {
        Map<String, Object> extraParams = new HashMap<>();
        extraParams.put("push_key", pushKey);
        extraParams.put("push_extra_value", pushExtraValue);
        return pushMessageParams("1", pushUserId, PushConstants.TITLE_SYSTEM, pushTime, pushContent, 4, "0", 4, "system", "system", extraParams);
    }

    public Map<String, Object> pushMessageCarMaintenanceParams(String pushUserId, String pushTime, String pushContent, Map<String, Object> extraParams) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_MAINTENANCE_WARNING, pushTime, pushContent, 5, extraParams);
    }

    public Map<String, Object> pushMessageCarMaintenanceForZTParams(String pushUserId, String pushTime, String pushContent, Map<String, Object> extraParams) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_MAINTENANCE_WARNING, pushTime, pushContent, 5, extraParams);
    }

    public Map<String, Object> pushMessageCarFuelGaugeParams(String pushUserId, String pushTime, String pushContent, Map<String, Object> extraParams) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_OIL_LEVEL_WARNING, pushTime, pushContent, 6, extraParams);
    }

    public Map<String, Object> pushMessageCarFaultParams(String pushUserId, String pushTime, String pushContent, Map<String, Object> extraParams) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_FAULT_WARNING, pushTime, pushContent, 7, extraParams);
    }

    public Map<String, Object> pushMessageTemperatureWarning(String pushUserId, String pushTime, String pushContent) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_TEMPERATURE_WARNING, pushTime, pushContent, 8, null);
    }

    public Map<String, Object> pushMessageHighHdlOilTemperatureWarning(String pushUserId, String pushTime, String pushContent) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_TEMPERATURE_WARNING, pushTime, pushContent, 8, null);
    }

    public Map<String, Object> pushMessageModelComparisonWarning(String pushUserId, String pushTime, String pushContent) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_MODELCOMPARISON_WARNING, pushTime, pushContent, 8, null);
    }

    public Map<String, Object> pushMessageNewOrOldMachineBrowseWarning(String pushUserId, String pushTime, String pushContent) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_NEWOROLD_MACHINE_BROSE_WARNING, pushTime, pushContent, 8, null);
    }

    public Map<String, Object> pushMessageLowHdlOilTemperatureWarning(String pushUserId, String pushTime, String pushContent) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_TEMPERATURE_WARNING, pushTime, pushContent, 8, null);
    }

    public Map<String, Object> pushMessageIdleTimeHighWarning(String pushUserId, String pushTime, String pushContent) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_IDLEHIGH_TIME_WARNING, pushTime, pushContent, 8, null);
    }

    public Map<String, Object> pushMessageLightSenceWarning(String pushUserId, String pushTime, String pushContent) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_LIGHT_SENCE_WARNING, pushTime, pushContent, 8, null);
    }

    public Map<String, Object> pushMessageServiceReminderWarning(String pushUserId, String pushTime, String pushContent) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_MAINTENANCE_WARNING, pushTime, pushContent, 8, null);
    }

    public Map<String, Object> pushMessagePressureWarning(String pushUserId, String pushTime, String pushContent) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_OIL_PRESSURE_WARNING, pushTime, pushContent, 8, null);
    }

    public Map<String, Object> pushMessageTemperatureHighWarning(String pushUserId, String pushTime, String pushContent) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_TEMPERATURE_HIGH_WARNING, pushTime, pushContent, 9, null);
    }

    public Map<String, Object> pushMessageCoupon(String pushUserId, String pushTime, String pushContent) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_COUPON, pushTime, pushContent, 10, null);
    }

    public Map<String, Object> carAnnual(String pushUserId, String pushTime, String pushContent) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_ANNUAL, pushTime, pushContent, 11, null);
    }


    private Map<String, Object> wrapPushMessageCarParams(String pushUserId, String title, String pushTime, String pushContent,
                                                         Integer contentType, Map<String, Object> extraParams) {
        return pushMessageParams("1", pushUserId, title, pushTime, pushContent, contentType, "0", 3, "system", "system", null, extraParams);
    }

    public Map<String, Object> pushMessageOrderIdParams(String pushUserId, String pushTime, String pushContent) {
        return pushMessageParams("1", pushUserId, PushConstants.TITLE_ORDER_REMIND, pushTime, pushContent, 8, "0", 2, "system", "system", null);
    }

    public Map<String, Object> pushMessageCarJumpUrlParams(String pushUserId, String pushTime, String pushContent) {
        return pushMessageParams("1", pushUserId, PushConstants.TITLE_SYSTEM, pushTime, pushContent, 9, "0", 3, "system", "system", null);
    }

    public Map<String, Object> pushMessageInsuranceParams(String pushUserId, String pushTime, String pushContent) {
        return pushMessageParams("1", pushUserId, PushConstants.TITLE_SYSTEM, pushTime, pushContent, 9, "0", 3, "system", "system", null);
    }

    public Map<String, Object> pushMessageCurrencyParams(String pushUserId, String pushTime, String pushContent) {
        return pushMessageParams("1", pushUserId, PushConstants.TITLE_SYSTEM, pushTime, pushContent, 10, "0", 5, "system", "system", null);
    }

    public Map<String, Object> pushMessageEveryNewsParams(String pushTime, String pushContent) {
        return pushMessageParams("0", "", PushConstants.TITLE_EVERY_DAY_NEWS, pushTime, pushContent, 11, "0", 5, "system", "system", null);
    }

    public Map<String, Object> pushMessageEveryHumanFace(String pushUserId, String pushTime, String pushContent) {
        return pushMessageParams("1", pushUserId, PushConstants.TITLE_SYSTEM, pushTime, pushContent, 9, "0", 3, "system", "system", null);
    }

    public Map<String, Object> pushMessageBehaviorParams(String pushObject, String pushTime, String pushContent, int behaviorType) {
        Map<String, Object> extraParams = new HashMap<>();
        extraParams.put("behavior_type", behaviorType);
        return pushMessageParams("1", pushObject, PushConstants.TITLE_HOT_CONENT, pushTime, pushContent, 12, "0", 5, "system", "system", extraParams);
    }

    public Map<String, Object> pushMessageAppPromotionActivitytrAfficParams(String pushTime, String pushContent) {
        return pushMessageParams("0", "", PushConstants.TITLE_APPPROMOTION_ACTIVITYTRAFFIC1, pushTime, pushContent, 11, "0", 5, "system", "system", null);
    }

    private Map<String, Object> pushMessageParamsHasId(String pushType, String pushUserId, String title, String pushTime, String pushContent,
                                                       Integer contentType, String pushStatus, Integer pushGroup, String createUser, String updateUser, Map<String, Object> extraParams) {

        Map<String, Object> params = pushMessageParams(pushType, pushUserId, title, pushTime, pushContent, contentType, pushStatus, pushGroup, createUser, updateUser, extraParams);
        params.put("id", UuidUtil.get32UUID());
        return params;
    }

    // 还款提醒 t_s_ss_message_record 的 send_type 为 107
    public Map<String, Object> pushMessageRemindRepaymentByDayParams(String pushUserId, String pushTime, String pushContent, Map<String, Object> extraParams) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_REMIND_REPAYMENT_WARNING, pushTime, pushContent, 107, extraParams);
    }

    // 黑区电子围栏报警 t_s_ss_message_record 的 send_type 为 113
    public Map<String, Object> pushMessageEnclosureBlack(String pushUserId, String pushTime, String pushContent, Map<String, Object> extraParams) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_ENCLOSURE_BLACK, pushTime, pushContent, 113, extraParams);
    }

    // 电子围栏报警 t_s_ss_message_record 的 send_type 为 7
    public Map<String, Object> pushMessageEnclosure(String pushUserId, String pushTime, String pushContent, Map<String, Object> extraParams) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_ENCLOSURE, pushTime, pushContent, 7, extraParams);
    }

    private Map<String, Object> pushMessageParams(String pushType, String pushUserId, String title, String pushTime, String pushContent,
                                                  Integer contentType, String pushStatus, Integer pushGroup, String createUser, String updateUser, Map<String, Object> extraParams) {
        return pushMessageParams(pushType, pushUserId,  title, pushTime, pushContent, contentType, pushStatus, pushGroup, createUser, updateUser,null, extraParams);
    }

    // 还款期前还款成功 的 send_type 为 200
    public Map<String, Object> pushMessageAccountingPeriod(String pushUserId, String pushTime, String pushContent, Map<String, Object> extraParams) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_REMIND_ACCOUNTING_PERIOD, pushTime, pushContent, 200, extraParams);
    }

    // 补缴欠款成功的消息类型为 201 (从200起始)
    public Map<String, Object> pushMessageSupplementaryPayment(String pushUserId, String pushTime, String pushContent, Map<String, Object> extraParams) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_REMIND_SUPPLEMENTARY_PAYMENT, pushTime, pushContent, 201, extraParams);
    }

    // 解锁申请已通过的消息类型为 202 (从200起始)
    public Map<String, Object> pushMessageApplicationPassed(String pushUserId, String pushTime, String pushContent, Map<String, Object> extraParams) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_REMIND_APPLICATIONPASSED, pushTime, pushContent, 202, extraParams);
    }

    // 解锁申请已拒绝的消息类型为 203 (从200起始)
    public Map<String, Object> pushMessageApplicationReject(String pushUserId, String pushTime, String pushContent, Map<String, Object> extraParams) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_REMIND_APPLICATIONREJECT, pushTime, pushContent, 203, extraParams);
    }

    // 电车的故障报警
    public Map<String, Object> pushMessageElectricCarFailParams(String pushUserId, String pushTime, String pushContent, Map<String, Object> extraParams) {
        return wrapPushMessageCarParams(pushUserId, PushConstants.TITLE_CAR_ELECTRIC_FAIL_WARNING, pushTime, pushContent, 204, extraParams);
    }

    /**
     * 生成消息记录参数
     *
     * @param pushType    推送的类型分类 0-所有人 1-个人 2-服务商 3-角色 4-设备
     * @param pushUserId  推送人的id
     * @param device      设备id
     * @param title       标题 1-系统通知
     * @param pushTime    推送时间
     * @param pushContent 推送内容
     * @param contentType 标识 0-无 1-今日工单速报 2-今日车辆速报 3-工单节点提醒推送 4-发布审核结果通知 5-车辆保养
     *                    6-车辆油位 7-车辆故障 8-工单流转 9-车辆电子围栏报警通知 10-发布消息通知管理员审核
     *                    11 每日新闻 12-行为热度提醒
     * @param pushStatus  标识推送状态 0-无 1-成功 2-失败
     * @param pushGroup   推送分组 1-营销推送 2-工单系统 3-车辆系统 4-B端商家 5-通知管理员
     * @param createUser
     * @param updateUser
     * @param extraParams
     * @return
     */
    private Map<String, Object> pushMessageParams(String pushType, String pushUserId, String title, String pushTime, String pushContent,
                                                  Integer contentType, String pushStatus, Integer pushGroup, String createUser, String updateUser, String device, Map<String, Object> extraParams) {
        Map<String, Object> params = new HashMap<>(16);
        params.put("push_type", pushType);
        params.put("push_user_id", pushUserId);
        params.put("device", device);

        params.put("title", title);
        params.put("push_time", pushTime);
        params.put("push_content", pushContent);
        params.put("content_type", contentType);
        params.put("push_status", pushStatus);
        params.put("push_group", pushGroup);
        params.put("create_user", createUser);
        params.put("update_user", updateUser);
        if (extraParams != null) {
            params.putAll(extraParams);
        }
        return params;
    }
}